/**
 * Copyright © 2015-2020 <a href="http://www.jeeplus.org/">JeePlus</a> All rights reserved.
 */
package com.jeeplus.modules.notes.web;

import java.util.List;

import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.google.common.collect.Lists;
import com.jeeplus.common.json.AjaxJson;
import com.jeeplus.core.web.BaseController;
import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.modules.notes.entity.JpNotes;
import com.jeeplus.modules.notes.service.JpNotesService;

/**
 * 办案手记Controller
 * @author 姜盼
 * @version 2020-09-20
 */
@RestController
@RequestMapping(value = "/notes/jpNotes")
public class JpNotesController extends BaseController {

	@Autowired
	private JpNotesService jpNotesService;

	@ModelAttribute
	public JpNotes get(@RequestParam(required=false) String id) {
		JpNotes entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = jpNotesService.get(id);
		}
		if (entity == null){
			entity = new JpNotes();
		}
		return entity;
	}


	/**
	 * 办案手记树表数据
	 */
	@GetMapping("list")
	public AjaxJson list(JpNotes jpNotes) {
		return AjaxJson.success().put("list", jpNotesService.findList(jpNotes));
	}

	/**
	 * 根据Id获取办案手记数据
	 */
	@GetMapping("queryById")
	public AjaxJson queryById(JpNotes jpNotes) {
		return AjaxJson.success().put("jpNotes", jpNotes);
	}

	/**
	 * 保存办案手记
	 */
	@PostMapping("save")
	public AjaxJson save(JpNotes jpNotes, Model model) throws Exception{
		/**
		 * 后台hibernate-validation插件校验
		 */
		String errMsg = beanValidator(jpNotes);
		if (StringUtils.isNotBlank(errMsg)){
			return AjaxJson.error(errMsg);
		}
		//新增或编辑表单保存
		jpNotesService.save(jpNotes);//保存
		return AjaxJson.success("保存办案手记成功");
	}

	/**
	 * 删除办案手记
	 */
	@DeleteMapping("delete")
	public AjaxJson delete(JpNotes jpNotes) {
		jpNotesService.delete(jpNotes);
		return AjaxJson.success("删除办案手记成功");
	}

	/**
	     * 获取JSON树形数据。
	     * @param extId 排除的ID
	     * @return
	*/
	@RequiresPermissions("user")
	@GetMapping("treeData")
	public AjaxJson treeData(@RequestParam(required = false) String extId) {
		List<JpNotes> list = jpNotesService.findList(new JpNotes());
		List rootTree = getJpNotesTree(list, extId);
		return AjaxJson.success().put("treeData", rootTree);
	}

	private List<JpNotes> getJpNotesTree(List<JpNotes> list, String extId) {
		List<JpNotes> jpNotess = Lists.newArrayList();
		List<JpNotes> rootTrees = jpNotesService.getChildren("0");
		for (JpNotes root : rootTrees) {
		    if (StringUtils.isBlank(extId) ||  !extId.equals(root.getId())) {
		        jpNotess.add(getChildOfTree(root, list, extId));
		    }
		}
		return jpNotess;
	}

	private JpNotes getChildOfTree(JpNotes jpNotes, List<JpNotes> jpNotesList, String extId) {
		jpNotes.setChildren(Lists.newArrayList());
		for (JpNotes child : jpNotesList) {
		    if (StringUtils.isBlank(extId) ||  (!extId.equals(child.getId()) && child.getParentIds().indexOf("," + extId + ",") == -1)) {
		        if (child.getParentId().equals(jpNotes.getId())) {
		            jpNotes.getChildren().add(getChildOfTree(child, jpNotesList, extId));
		        }
		    }
		}
		return jpNotes;
	}



}